package com.sg.business.commons.labelprovider.complex;

import org.bson.types.ObjectId;
import org.eclipse.ui.forms.IFormPart;

import com.mobnut.db.model.ModelService;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Folder;
import com.sg.widgets.commons.labelprovider.ObjectIdLabelProvider;
import com.sg.widgets.part.editor.fields.AbstractFieldPart;

public class FolderPathLabelProvider extends ObjectIdLabelProvider {

	@Override
	protected Class<? extends PrimaryObject> getModelClass() {
		return Folder.class;
	}

	@Override
	public String getText(Object element) {
		if (element instanceof PrimaryObject) {
			Object value = getValue(element);
			if (value instanceof ObjectId) {
				Folder po = ModelService.createModelObject(
						Folder.class, (ObjectId) value);
				return po.getPath(-1);
			}
		}else if(element instanceof ObjectId){
			Folder po = ModelService.createModelObject(
					Folder.class, (ObjectId) element);
			return po.getPath(-1);
		}

		return ""; //$NON-NLS-1$
	}
	
	@Override
	public String getPresentValue(IFormPart part) {
		Object value = ((AbstractFieldPart) part).getValue();
		if (value instanceof ObjectId) {
			Folder po = ModelService.createModelObject(Folder.class,
					(ObjectId) value);
			if (po != null) {
				return po.getPath(-1);
			}
		}
		return ""; //$NON-NLS-1$
	}
}
